package Utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/*
 * 
 * This Class is used for send sms with the help of getHelper class
 */
public class SendSMSHelper {

	static long TimeStamp;
	static String md5Part;
	static String codestr;
	static String result;
	static String url;
	
	/**
	 * 生成发送短信的XML字符串内容
	 */
	public static String createSmsSendXml(Map<String, String> map) {
		StringPlus sp = new StringPlus();
		sp.append(String
				.format("<Group Login_Name=\"%s\" Login_Pwd=\"%s\" InterFaceID=\"\" OpKind=\"0\">",
						map.get("LOGIN_NAME"), map.get("PASSWORD")));
		sp.append(String.format("<E_Time>%s</E_Time>", map.get("START_TIME")));
		sp.append("<Item>");
		sp.append("<Task>");
		sp.append(String.format(
				"<Recive_Phone_Number>%s</Recive_Phone_Number>",
				map.get("MOBILE")));
		sp.append(String.format("<Content><![CDATA[%s]]></Content>",
				map.get("MESSAGE")));
		sp.append(String.format("<Search_ID>%s</Search_ID>", map.get("TASK_ID")));
		sp.append("</Task>");
		sp.append("</Item>");
		sp.append("</Group>");
		return sp.value();
	}

	public static boolean send(String msg, String mobileNum) {

		try {
			codestr = URLEncoder.encode(msg, "GBK");

			TimeStamp = System.currentTimeMillis() / 1000; // the unit is second

			md5Part = MD5Helper.getMD5Str("123456_" + TimeStamp + "_topsky");

			url = "http://115.182.33.133/houtai/sms.php?" + "cpid=4617&"
					+ "password=" + md5Part + "&channelid=3028&" + "tele="
					+ mobileNum + "&" + "msg=" + codestr + "&" + "timestamp="
					+ TimeStamp;

			result = GetHelper.doHttpGet(url);
			System.out.println("sms result: " + result);
			// result="success:322232";
			if (String.valueOf(result.charAt(0)).equals("s"))
				return true;
			else
				return false;

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

	}

	/*
	 * return balance value of SMS platform ���ض���ƽ̨���
	 */
	public static float getBalance() {

		TimeStamp = System.currentTimeMillis() / 1000; // the unit is second

		md5Part = MD5Helper.getMD5Str("123456_" + TimeStamp + "_topsky");

		url = "http://115.182.33.133/houtai/sms_ye.php?" + "userid=4617&"
				+ "password=" + md5Part + "&channelid=3028&" + "timestamp="
				+ TimeStamp;

		result = GetHelper.doHttpGet(url);
		System.out.println("sms result: " + result);
		// result="success:322232";
		if (String.valueOf(result.charAt(0)).equals("s")) {
			// System.out.println(Float.parseFloat(
			// result.substring(result.indexOf(":")+1)));
			return Float.parseFloat(result.substring(result.indexOf(":") + 1));
		} else {
			System.out.println("Error");
			return -1;
		}

	}

	public static boolean newSend(String msg, String mobileNum) {
		try {
			codestr = URLEncoder.encode(msg, "GBK");

			TimeStamp = System.currentTimeMillis() / 1000; // the unit is second

			md5Part = MD5Helper.getMD5Str("youbox123456_" + TimeStamp + "_topsky");

			url = "http://115.182.33.133/houtai/sms.php?" + "cpid=6956&"
					+ "password=" + md5Part + "&channelid=3028&" + "tele="
					+ mobileNum + "&" + "msg=" + codestr + "&" + "timestamp="
					+ TimeStamp;

			result = GetHelper.doHttpGet(url);
			System.out.println("sms result: " + result);
			// result="success:322232";
			if (String.valueOf(result.charAt(0)).equals("s"))
				return true;
			else
				return false;

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
public static long send720Sms(String msg,String phone) {
		
		String taskId=GenerateRandNumHelper.generate(4)+phone;
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("LOGIN_NAME", "fj720_01");
		map.put("PASSWORD", MD5Helper.getMD5Str("ab8888").toUpperCase());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		map.put("START_TIME", sdf.format(new Date()));
		map.put("MOBILE", phone);
		map.put("MESSAGE", msg);
		map.put("TASK_ID", taskId );
		String xml=createSmsSendXml(map);
		String response=Http.post("http://userinterface.vcomcn.com/Opration.aspx",
				xml);
//		String response=Http.post("http://www.vcomcn.cn/",
//				xml);
		if(response.equals("00")){
			System.out.println("成功");
			return Long.parseLong(taskId);
		}else{
			//短信发送失败
			System.out.println("失败");
			return -1;
		}
	}

	public static void main(String[] args) {
		// send("你好", "18960877052");
		// getBalance();
		send720Sms("您的中通快件,桂苑11,取件号:15.在17:20前领取.单号:889707986978 服务电话:18650346254",
				"13559455537");
	}

}
